package com.paranoidvulture.javaEngine.Views;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

import com.paranoidvulture.javaEngine.MainPackage.ScreenManager;




public class Menu extends ViewBase{

	private String[] menuItems;
	private int selectedIndex;
	private boolean selectedOption;
	
	
	public Menu(ScreenManager s, String[] menuItems) {
		super(s);
		this.menuItems = menuItems;
		this.setName("Menu");
	}

	public void select(){
		this.selectedOption = false;
	}
	
	public void init(){
		measureMenu();
		selectedOption = false;
	}
	
	public void update(){
		selectedOption = false;
	}
	
	public void draw(Graphics2D g){
		if (this.getEnabled()){
			 Color hilite = Color.blue;
			 float locationX = this.getPosX();
			 float locationY = this.getPosY();
			try{
				Window w = s.getFullScreenWindow();
				g.setColor(w.getBackground());
				g.fillRect(0, 0, s.getWidth(), s.getHeight());
				g.setColor(w.getForeground());
				for (int i = 0; i < this.menuItems.length; i++){
					if (i == selectedIndex)
						g.setColor(hilite);
					else g.setColor(Color.white);
					g.drawString(this.menuItems[i], locationX , locationY);
					
					locationY += 20;
				}
			}catch (Exception ex){}
		}
	}
	
	public boolean getSelectedOption(){
		return selectedOption;
	}
	
	public int getSelectedIndex(){
		return selectedIndex;
	}
	
	public void setSelectedIndex(int index){
		this.selectedIndex = index;
		if (selectedIndex < 0)
			selectedIndex = 0;
		if (selectedIndex > this.menuItems.length -1)
			selectedIndex = this.menuItems.length - 1;
	}
	
	private void measureMenu(){
		this.setHeight(0);
		this.setWidth(0);
		for (int i = 0; i < this.menuItems.length - 1; i++){
			if (this.menuItems[i].length() * 10 > this.getWidth())
				this.setWidth(this.menuItems[i].length()*10);
			this.setHeight(this.getHeight() + 5);
		}
		Window w = s.getFullScreenWindow();
		this.setPosX((w.getWidth() - this.getWidth()) / 2);
		this.setPosY((w.getHeight() - this.getHeight()) / 2);
		
	}
	



	
	public void keyPressed(KeyEvent e) {
		if (this.getEnabled()){
			int keyCode = e.getKeyCode();
			if(keyCode == KeyEvent.VK_DOWN){
				setSelectedIndex(getSelectedIndex()+1);
			}else if (keyCode == KeyEvent.VK_UP){
				setSelectedIndex(getSelectedIndex()-1);			
			}else if (keyCode == KeyEvent.VK_ENTER){
				selectedOption = true;
			}
		}
	}
	
	public void keyReleased(KeyEvent e) {
		if (this.getEnabled()){
			e.consume();
		}
	}

	public void keyTyped(KeyEvent e) {
		if (this.getEnabled()){	
			e.consume();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
